// Copyright (c) 2014-2017 Bytter. All rights reserved.
// ============================================================================
// CURRENT VERSION
// ============================================================================
// CHANGE LOG
// V2.3 : 2017年4月17日, liuwl, creation
// ============================================================================
package com.betterjr.modules.contract.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.betterjr.common.mapper.BeanMapper;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterStringUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.entity.CustOperatorInfo;
import com.betterjr.modules.contract.dao.ContractTemplateStampPlaceMapper;
import com.betterjr.modules.contract.data.ContractTempStampPlaceData;
import com.betterjr.modules.contract.entity.ContractTemplateStampPlace;

/**
 * @author liuwl
 *
 */
@Service
public class ContractTempStampPlaceService
        extends BaseService<ContractTemplateStampPlaceMapper, ContractTemplateStampPlace> {
    public static String[] signatorys = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

    /**
     *
     * @param templateId
     * @param anSequence
     * @return
     */
    private ContractTemplateStampPlace findStampPlaceBySeq(final Long anTemplateId, final Integer anSequence) {
        BTAssert.notNull(anTemplateId, "模板编号不允许为空！");
        BTAssert.notNull(anSequence, "签章位置序号不允许为空！");

        final Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("templateId", anTemplateId);
        conditionMap.put("sequence", anSequence);

        return Collections3.getFirst(this.selectByProperty(conditionMap));
    }

    /**
     * 保存印章位置
     * 
     * @param ContractTemplateId
     * @param anStampPlace
     * @return
     */
    protected void saveKeyWordStampPlace(final Long anTemplateId, final String anTemplateName, final Integer anSequence,
            final String anKeyWord, final Long anAxisX, final Long anAxisY, final Integer temporaryCount) {
        // temporaryCount为暂时对接xuyp，后期改为anSequence
        final ContractTemplateStampPlace stampPlace = findStampPlaceBySeq(anTemplateId, temporaryCount);

        if (stampPlace == null) {
            saveInitStampPlace(anTemplateId, anTemplateName, anSequence, anKeyWord, anAxisX, anAxisY, temporaryCount);
        } else {
            stampPlace.setKeyWord(anKeyWord);

            stampPlace.setAxisX(anAxisX);
            stampPlace.setAxisY(anAxisY);

            stampPlace.modify(UserUtils.getOperatorInfo());

            final int result = this.updateByPrimaryKeySelective(stampPlace);
            BTAssert.isTrue(result == 1, "设置印章位置失败！");
        }
    }

    /**
     * 删除印章位置
     * 
     * @param ContractTemplateId
     * @param anStampPlaceId
     * @return
     */
    private void saveInitStampPlace(final Long anTemplateId, final String anTemplateName, final Integer anIndex,
            final String anKeyWord, final Long anAxisX, final Long anAxisY, final Integer temporaryCount) {
        final CustOperatorInfo operator = UserUtils.getOperatorInfo();
        final ContractTemplateStampPlace stampPlace = new ContractTemplateStampPlace();

        stampPlace.setTemplateId(anTemplateId);
        stampPlace.setTemplateName(anTemplateName);
        stampPlace.setSignatory(signatorys[anIndex.intValue()]);
        // temporaryCount为暂时对接xuyp，2开始递减。后期改递增时，把签署顺序改为anIndex。
        stampPlace.setSequence(temporaryCount);
        stampPlace.setKeyWord(anKeyWord);
        stampPlace.setAxisX(anAxisX);
        stampPlace.setAxisY(anAxisY);
        stampPlace.init(operator);

        final int result = this.insert(stampPlace);
        BTAssert.isTrue(result == 1, "初始化印章位置失败！");
    }

    /**
     * 获取模板 印章 位置列表
     * 
     * @param ContractTemplateId
     * @return
     */
    protected List<ContractTemplateStampPlace> queryStampPlace(final Long anTemplateId) {
        BTAssert.notNull(anTemplateId, "模板编号不允许为空！");

        final Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("templateId", anTemplateId);

        return this.selectByProperty(conditionMap, "sequence");
    }

    public ContractTempStampPlaceData findStampPlaceData(final Long anTemplateId, final Integer anOrder) {
        final ContractTemplateStampPlace tmpPlace = findStampPlaceBySeq(anTemplateId, anOrder);
        ContractTempStampPlaceData result = null;
        if (tmpPlace == null) {
            result = new ContractTempStampPlaceData();
        } else {
            result = BeanMapper.map(tmpPlace, ContractTempStampPlaceData.class);
        }

        return result;
    }

    /**
     * 
     * @Title: saveCustomLocation 
     * @Description: 自定义合同插入签章位置 
     * @param anParamMap 签章位置   
     * @param anContractProtocoId 协议清单编号
     * @param standardTypeName  协议清单名称
     * @param signatoryLeng  签署方总计
     * @throws 
     * @author xuzx
     * @date 2017年11月5日 下午5:11:57
     */
    public void saveCustomLocation(final Map<String, Object> anParamMap, final Long anContractProtocoId,
            final String standardTypeName, final int signatoryLeng) {
        // 签章总数
        int anParamLength = (anParamMap.size() - 1) / 5;
        BTAssert.isTrue(anParamLength == signatoryLeng, "签署方总数和签章总数不匹配");
        queryCountContractSignatory(anContractProtocoId);
        final CustOperatorInfo operator = UserUtils.getOperatorInfo();
        int countSignatory = queryCountContractSignatory(anContractProtocoId);
        if (countSignatory != 0) {
            delAssociatedLocation(anContractProtocoId);
        }
        for (int i = 0; i < signatoryLeng; i++) {
            ContractTemplateStampPlace contractStampPlace = new ContractTemplateStampPlace();
            contractStampPlace.init(operator);
            contractStampPlace.setTemplateId(anContractProtocoId);
            contractStampPlace.setTemplateName(standardTypeName);
            contractStampPlace.setPagination(String.valueOf(anParamMap.get("param[" + i + "][pagination]")));
            contractStampPlace.setSignatory(String.valueOf(anParamMap.get("param[" + i + "][signatory]")));
            contractStampPlace.setSequence(i);
            contractStampPlace.setAxisX(BetterStringUtils.toLong(anParamMap.get("param[" + i + "][axisX]")));
            contractStampPlace.setAxisY(BetterStringUtils.toLong(anParamMap.get("param[" + i + "][axisY]")));
            this.insert(contractStampPlace);
        }
    }

    /**
     * 
     * @Title: delAssociatedLocation 
     * @Description: 自定义协议删除签章位置 
     * @param anContractProtocoListId 自定协议编号
     * @return 删除签章位置
     * @throws 
     * @author xuzx
     * @date 2017年11月5日 下午5:21:31
     */
    public int delAssociatedLocation(final Long anContractProtocoListId) {
        Map<String, Object> anMap = new HashMap<String, Object>(1);
        anMap.put("templateId", anContractProtocoListId);
        return this.deleteByExample(anMap);
    }

    /**
     * 
     * @Title: queryCountContractSignatory 
     * @Description: 查看签署方总计
     * @param anContractProtocoId 协议清单编号
     * @return 查看签署方总计
     * @throws 
     * @author xuzx
     * @date 2017年11月5日 下午5:31:12
     */
    public int queryCountContractSignatory(final Long anContractProtocoId) {
        Map<String, Object> anMap = new HashMap<String, Object>(1);
        anMap.put("templateId", anContractProtocoId);
        return this.selectByClassProperty(ContractTemplateStampPlace.class, anMap).size();

    }

    /**
     * 
     * @Title: queryDetailContractSignatory 
     * @Description: 查看协议清单签章详细信息
     * @param anContractProtocoId 协议清单编号
     * @return 查看协议清单签章详细信息
     * @throws 
     * @author xuzx
     * @date 2017年11月5日 下午6:45:32
     */
    public List<ContractTemplateStampPlace> queryDetailContractSignatory(final Long anContractProtocoId) {
        Map<String, Object> anMap = new HashMap<String, Object>(1);
        anMap.put("templateId", anContractProtocoId);
        return this.selectByClassProperty(ContractTemplateStampPlace.class, anMap);

    }
}
